package com.dancun.controller;


import com.dancun.entity.Problem;
import com.dancun.entity.Solve;
import com.dancun.service.IProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController
public class ProblemController {
    @Autowired
    IProblemService problemService;


    public List<Problem> listByName(String name) {
        return problemService.listByName(name);
    }

    @RequestMapping("ProblemDel")
    public ModelAndView ProblemDel() {
        ModelAndView mav = new ModelAndView("/Main/ProblemListDel.jsp");
        List<Problem> data = problemService.list();
        if (data.size() != 0) {
            for (Problem problem : data) {
                if (problem.getIsSolve() != null) {
                    if (problem.getIsSolve() == 0) {
                        mav.addObject("proflag", 1);
                        break;
                    } else {
                        mav.addObject("proflag", 0);
                    }
                }
            }
        } else {
            mav.addObject("proflag", 0);
        }
        mav.addObject("data", data);
        return mav;
    }

    @RequestMapping(value = "/listProblem")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("/Main/ProblemList.jsp");
        List<Problem> data = problemService.list();
        if (data.size() != 0) {
            for (Problem problem : data) {
                if (problem.getIsSolve() != null) {
                    if (problem.getIsSolve() == 0) {
                        mav.addObject("proflag", 1);
                        break;
                    } else {
                        mav.addObject("proflag", 0);
                    }
                }
            }
        } else {
            mav.addObject("proflag", 0);
        }
        mav.addObject("data", data);
        return mav;
    }

    @RequestMapping("/proAdd")
    public ModelAndView save(Problem problem) {
        problem.setDate(LocalDateTime.now());
        problem.setIsSolve(0);
        problem.setPid((String.valueOf((Math.random() * 100))));

        if (problemService.save(problem)) {
            return new ModelAndView("/listProblem");
        } else {
            return new ModelAndView("/Main/Problem.jsp");
        }

    }

    @RequestMapping("/findProblem")
    public ModelAndView find(Problem problem) throws IOException, ServletException {
        ModelAndView mav = new ModelAndView("/Main/ProblemList.jsp");
        List<Problem> data = problemService.listByName("%" + problem.getName() + "%");
        if (data.size() != 0) {
            for (Problem p : data) {
                if (p.getIsSolve() != null) {
                    if (p.getIsSolve() == 0) {
                        mav.addObject("proflag", 1);
                        break;
                    } else {
                        mav.addObject("proflag", 0);
                    }
                }
            }
        } else {
            mav.addObject("proflag", 0);
        }
        mav.addObject("data", data);
        return mav;
    }



    @RequestMapping("delProblem")
    @ResponseBody
    public Map del(Problem problem) {
        boolean state = false;
        Map map = new HashMap();
        try{
            problemService.removeById(problem.getPid());
            state=true;
            map.put("message","");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message",e.getStackTrace().toString());
        }
        map.put("state",state);

        map.put("data","");

        return  map;
    }

}
